package com.dingding.service.activity.core.biz.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.ActivityRelevance;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.activity.core.biz.IActivityRelUserService;
import com.dingding.service.activity.core.dao.IActivityRelUserDao;
import com.dingding.service.activity.core.dao.IActivityRelevanceDao;
import com.dingding.service.activity.core.dao.ICouponRelActivityDao;
import com.dingding.service.activity.core.dao.IUserCouponDao;
import com.dingding.service.common.core.dao.IUserLoginDao;

/**
 * 
 * @ClassName: IActivityRelUserService 
 * @Description: 用户和活动关联关系业务处理类
 * @author CHEHAUNBO 
 * @date 2016年4月5日 上午11:21:57
 * 
 */
@Component("activityRelUserService")
public class ActivityRelUserServiceImpl implements IActivityRelUserService {

	@Resource(name = "activityRelUserDao")
	private IActivityRelUserDao actvityRelUserDao;

	@Resource(name = "activityRelevanceDao")
	private IActivityRelevanceDao activityRelevanceDao;
	
	@Resource(name = "couponRelActivityDao")
	private ICouponRelActivityDao couponRelActivityDao;
	
	@Resource(name="userCouponDao")
	private IUserCouponDao userCouponDao;
	
	@Resource(name = "userLoginDao")
	private IUserLoginDao userLoginDao;
	
	/**
	 * 
	 * @Title: addAttendActivityNumber 
	 * @Description: 修改用户参加活动次数
	 * @param userId 用户ID 
	 * @param activityId 活动ID 
	 * @param type 活动类型 1：关联积分活动 2:关联优惠券活动
	 * @return boolean  true 成功 false 失败 
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	public boolean addAttendActivityNumber(String userId, String activityId, int type) {
		return actvityRelUserDao.addAttendActivityNumber(userId, activityId, type);
	}

	/**
	 * @Title: exchangeActivity 
	 * @Description: 兑换邀请码
	 * @param userId 用户Id
	 * @param invitationCode 邀请码
	 * @author YHQ 
	 * @return Integer    返回类型
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Integer exchangeActivity(String userId, String invitationCode) {
		
		//根据邀请码获取兑换邀请码信息
		ActivityRelevance activityRelevance = activityRelevanceDao.getActivityRelevance(invitationCode);
		
		//判断根据邀请码获取的信息是否为空
		if(activityRelevance == null 
				|| ToolUtil.isBlank(activityRelevance.getActivityId()) 
				|| activityRelevance.getIsAttendActivity() == null){
			return SystemCode.EXCHANGE_FAIL_ERROR;
		}
		
		//获取兑换人的兑换邀请码信息
		ActivityRelevance activityRelevanceById = activityRelevanceDao.getActivityRelevanceByUserId(userId,activityRelevance.getActivityId());
		
		//根据邀请码获取要赠送的优惠券信息
		Object[] object = couponRelActivityDao.getCouponRelActivityInfo(activityRelevance.getActivityId());
		
		if(object != null && object.length > 0){
			
			boolean flag = userCouponDao.addTwoCoupon(object,activityRelevance,activityRelevanceById,userId);
			
			if(flag){

				//邀请人数加一
				activityRelevance.setActivityNumber(activityRelevance.getActivityNumber() + 1);
				//是否有效 1 未参与， 2 已参与
				if(activityRelevance.getIsAttendActivity() == 1){
					activityRelevance.setIsAttendActivity(2);
				}
				
				//修改兑换信息状态
				activityRelevanceDao.updateActivityRelevance(activityRelevance);
				
				//判断当前用户是否存在邀请码，不存在则生成
				if(activityRelevanceById == null){
					activityRelevanceById = activityRelevanceDao.addActivityRelevance(userId,activityRelevance.getActivityId());
				}else if(activityRelevanceById.getIsAttendActivity() != null && activityRelevanceById.getIsAttendActivity() == 2){
					
					return SystemCode.REPEAT_EXCHANGE_ERROR;
				}
				
				if(activityRelevance != null && activityRelevance.getIsAttendActivity() == 1){
					userLoginDao.updateCouponNumber(activityRelevance.getUserId());
				}
				
				if(activityRelevanceById != null && activityRelevanceById.getIsAttendActivity() == 1){
					userLoginDao.updateCouponNumber(userId);
					
					activityRelevanceById.setActivityNumber(activityRelevanceById.getActivityNumber() + 1);
					//是否有效 1 未参与， 2 已参与
					if(activityRelevanceById.getIsAttendActivity() == 1){
						activityRelevanceById.setIsAttendActivity(2);
					}
					
					//修改兑换信息状态
					activityRelevanceDao.updateActivityRelevance(activityRelevanceById);
				}
				
				return SystemCode.SUCCESS;
			}
		}
		
		return SystemCode.EXCHANGE_FAIL_ERROR;
	}

}
